﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;

public partial class tryregistration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillYOP();
        }

    }

    private void fillYOP()
    {
        ddlYOP.Items.Clear();
        for (int i = 2011; i >= 2000; i--)
        {
            ddlYOP.Items.Add(new ListItem(i.ToString(),i.ToString()));
        }
    }

    private void clearAll()
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtEmail.Text = "";
        txtMobile.Text = "";
        txtConfirmMail.Text = "";
        txtHighestPer.Text = "";
        ddlYOP.SelectedIndex = -1;
        ddlEducation.SelectedIndex = -1;
        lablMess.Text = "";
        lablNote.Text = "";
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearAll();
    }

    public static bool SendEmail(string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, System.Web.Mail.MailFormat pFormat, string pAttachmentPath)
    {
        try
        {
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");

            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //Use 0 for anonymous
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                pGmailEmail);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                 pGmailPassword);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 "true");
            myMail.From = pGmailEmail;
            myMail.To = pTo;
            myMail.Subject = pSubject;
            myMail.BodyFormat = pFormat;
            myMail.Body = pBody;


            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(myMail);
            return true;
        }
        catch (Exception ex)
        {
            throw;
        }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int sendNews;
        DataTable dtRegistrationNo = new DataTable();
        if (chkSendNewsletter.Checked == true)
        {
            sendNews = 1;
        }
        else
        {
            sendNews = 0;
        }
        dtRegistrationNo=GenericDAL.SetRegistrationInformation(ddlSalutation.SelectedItem.Text,txtFirstName.Text, txtLastName.Text, txtEmail.Text, txtMobile.Text, ddlEducation.SelectedItem.Text, ddlYOP.SelectedItem.Text, txtHighestPer.Text,sendNews);

        StringBuilder sbMail = new StringBuilder();
        sbMail.Append("<table width='70%'><tr><td>Registration No:<b> " + dtRegistrationNo.Rows[0][0].ToString() + "</b></td></tr>");
        sbMail.Append("<tr><td>First Name:<b> " + txtFirstName.Text + "</b></td></tr><tr><td></td></tr>");
        sbMail.Append("<tr><td>Last Name Id:<b> " + txtLastName.Text + "</b></td></tr></table>");
        sbMail.Append("<tr><td>Email Id:<b> " + txtEmail.Text + "</b></td></tr></table>");
        sbMail.Append("<tr><td>Mobile No:<b> " + txtMobile.Text + "</b></td></tr></table>");
        sbMail.Append("<tr><td>Qualification:<b> " + ddlEducation.SelectedItem.Text + "</b></td></tr></table>");
        sbMail.Append("<tr><td>Year Of Passout:<b> " + ddlYOP.SelectedItem.Text + "</b></td></tr></table>");
        sbMail.Append("<tr><td>Higest Degree Percentage:<b> " + txtHighestPer.Text + "</b></td></tr></table>");

        ConfigurationManager.AppSettings["ContactEmailTo"] = "contact@skilledres.com";
        if (SendEmail(ConfigurationManager.AppSettings["ContactEmailFrom"], ConfigurationManager.AppSettings["ContactEmailPassword"], ConfigurationManager.AppSettings["ContactEmailTo"], "\"" + txtFirstName.Text + " " + txtLastName.Text + "\" Registerd For .Net Job Oriented Training", sbMail.ToString(), System.Web.Mail.MailFormat.Html, "") == true)
        {

        }

        StringBuilder sbMailUser = new StringBuilder();
        sbMailUser.Append("<table width='70%'><tr><td><b>Congrats, " + ddlSalutation.SelectedItem.Text+" "+txtFirstName.Text+" "+txtLastName.Text+
            ", Your Registration For .Net Job Oriented Training is Completed Successfully." + "</b></td></tr>");
        sbMailUser.Append("<tr><td><b> Your registration Number Is " +dtRegistrationNo.Rows[0][0].ToString()+ "</b></td></tr><tr><td></td></tr>");
        sbMailUser.Append("<tr><td><b> Please Remember Your Registration Number for future Enquiry"+ "</b></td></tr><tr><td></td></tr>");
        sbMailUser.Append("<tr><td><b> Thanks & Regards," + "</b></td></tr><tr><td></td></tr>");
        sbMailUser.Append("<tr><td><b> contact@skilledres.com" + "</b></td></tr><tr><td></td></tr>");

        ConfigurationManager.AppSettings["ContactEmailTo"] = txtEmail.Text;
        if (SendEmail(ConfigurationManager.AppSettings["ContactEmailFrom"], ConfigurationManager.AppSettings["ContactEmailPassword"], ConfigurationManager.AppSettings["ContactEmailTo"], "Regarding SkilledRES .Net Job Oriented Training", sbMailUser.ToString(), System.Web.Mail.MailFormat.Html, "") == true)
        {

        }

        clearAll();
        lablMess.Text = "Congrats, Your Registration is Completed Successfully, Your Registration Number is "+dtRegistrationNo.Rows[0][0].ToString();
        lablNote.Text = "Note: Please Remember Your Registration Number for future Communication.";
    }
}